<template>
	<view class="box">
		<view class="img-box">
			<image :src="userInfo.avatar" mode="" class="img" v-if="userInfo.avatar"></image>
			<image src="@/static/mine/kllz.png" mode="" class="img" v-else></image>
			<text class="name">{{userInfo.userName}}</text>
		</view>
		<view class="content">
			<view class="title">转入金额</view>
			<view class="input-box">
				<input class="input" type="number" placeholder-class="placeholder" placeholder="请输入金额"
					v-model="amount"></input>
			</view>
			<view class="balance">
				{{'当前消费券：'+ userInfo.balance}}
			</view>
			<view class="btn" @tap="submit">
				确认
			</view>
		</view>
		<uni-popup ref="passwordPopup" :mask-click="false">
			<view class="buyProp">
				<uni-icons type="closeempty" size="22" class="closeImg" @tap="closePasswordProp"></uni-icons>
				<view class="title">请输入密码</view>
				<view class="numTitle">支付密码</view>
				<!-- <uni-easyinput type="password" v-model="tradePasswd" primaryColor="#FA3C42" class="input-prop"
					placeholder="请输入支付密码" :inputBorder="false" placeholderStyle="font-size: 26rpx;"></uni-easyinput> -->
				<view class="input-tradePasswd">
					<input placeholder="请输入支付密码" type="password" class="input-prop" placeholder-class="placeholder" primaryColor="#FA3C42"
						v-model="tradePasswd" />
				</view>
				<view class="sures" @tap="sure">确 定</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		transferToKllzService,
		getTransferToTxUserNameAvatar
	} from "@/api/api.js"
	export default {
		data() {
			return {
				qrCode: '',
				amount: '',
				tradePasswd: '',
				userInfo: {},
				clickCount: 0, // 用于记录点击次数
			};
		},
		onLoad(query) {
			// 在页面加载时，从 query 参数中获取扫码结果
			if (query.data) {
				this.qrCode = decodeURIComponent(query.data);
				console.log(this.qrCode)
				this.getTransferToTxUserNameAvatar(this.qrCode)
			}
		},
		onShow() {

		},
		methods: {
			getTransferToTxUserNameAvatar(qrCode) {
				getTransferToTxUserNameAvatar({
					qrCode: qrCode
				}).then(res => {
					console.log(res);
					this.userInfo = res.data;
				})
			},
			submit() {
				if (!this.amount || this.amount == 0) {
					uni.showToast({
						icon: 'none',
						title: '请输入有效金额'
					})
					return
				}
				this.$refs.passwordPopup.open()
			},
			closePasswordProp() {
				this.tradePasswd = "";
				this.$refs.passwordPopup.close()
			},
			sure() {
				if (!this.tradePasswd) {
					uni.showToast({
						icon: 'none',
						title: '密码不可为空'
					})
					return
				}
				this.debouncedClick()
			},
			transferToKllzService() {
				let data = {
					amount: this.amount,
					qrCode: this.qrCode,
					tradePasswd: this.tradePasswd
				}
				console.log(data)
				transferToKllzService(data).then(res => {

					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '转账成功'
						})
						this.$refs.passwordPopup.close()
						uni.switchTab({
							url: '/pages/goodsIndex/index'
						})
					}
				})
			},
			handleClick() {
				this.clickCount++;
			},
			debouncedClick() {
				clearTimeout(this.debounceTimer);
				this.debounceTimer = setTimeout(() => {
					this.transferToKllzService();
				}, 300);
			},
		}
	}
</script>

<style lang="less" scoped>
	.box {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background: #F6F6F6;

		.img-box {
			// height: 300rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;

			.img {
				width: 112rpx;
				height: 112rpx;
				margin-top: 40rpx;
			}

			.name {
				font-weight: 500;
				font-size: 40rpx;
				color: #151822;
				margin: 28rpx 0 40rpx 0;
			}
		}

		.content {
			flex: 1;
			padding: 40rpx;
			background-color: #fff;
			border-radius: 32rpx 32rpx 0rpx 0rpx;

			.title {
				font-weight: 500;
				font-size: 32rpx;
				color: #151822;
				margin-bottom: 60rpx;
			}

			.input-box {
				border-bottom: 2rpx solid #EEEEEE;
				padding-bottom: 30rpx;
			}

			.balance {
				font-weight: 400;
				font-size: 28rpx;
				color: #23C87E;
				margin-top: 28rpx;
			}

			.btn {
				width: 680rpx;
				height: 90rpx;
				background: #00C538;
				border-radius: 10rpx;
				text-align: center;
				line-height: 90rpx;
				color: #fff;
				margin: 140rpx auto;
			}
		}

		.placeholder {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 40rpx;
			color: #808080;
			line-height: 40rpx;
		}

		.buyProp {
			padding-bottom: 40rpx;
			position: relative;
			display: flex;
			align-items: center;
			flex-direction: column;
			width: 560rpx;
			background: #FFFFFF;
			border-radius: 20rpx;

			.closeImg {
				position: absolute;
				top: 20rpx;
				right: 25rpx;
			}

			.title {
				margin-top: 38rpx;
				font-size: 30rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #1A1A1A;
				line-height: 42rpx;
			}

			.numTitle {
				margin-top: 46rpx;
				width: 470rpx;
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #000000;
				line-height: 37rpx;
				margin-left: 30rpx;
			}
			.input-tradePasswd{
				border-bottom: 1px solid #808080;
			}
			.input-prop {
				margin-top: 12rpx;
				height: 68rpx;
				width: 470rpx;
				font-size: 24rpx;
			}

			.placeholder {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #808080;
				line-height: 33rpx;
			}

			.sures {
				margin-top: 60rpx;
				text-align: center;
				line-height: 86rpx;
				width: 470rpx;
				height: 86rpx;
				background: linear-gradient(90deg, #FF6047 0%, #FA2E2C 100%);
				border-radius: 4rpx;
				font-size: 28rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #fff;
				border-radius: 16rpx;
			}

			.codes {
				position: absolute;
				right: 43rpx;
				bottom: 210rpx;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: var(--tm-themeColor-level1-1);
				line-height: 33rpx;
			}
		}
	}
</style>